// pages/caseReward/entrustReward/entrustReward.js
import request from '../../../utils/config.js';
import formatUtil from '../../../utils/util.js';
import cityData from '../../../utils/vendor/cityList.js';
const app = getApp();
let provinceList = ['全国'];//获取省级列表
for (let i in cityData) {
  provinceList.push(i); 
}
let lastCityList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: wx.getSystemInfoSync().windowHeight - 40,
    // provinceList: [],
    // cityList:[],
    p_c: [[""], [""]],//对应的数组下标 eg: pcIndex[1,2]指的是 p_c[0][1] 省的名称 和 p_c[1][2] 市的名称
    pcIndex: [0, 0],
    indexType:"",
    consultFileList:[
      { name: '委托类型', val: '' },
      { name:'立案',val:'1'},
      { name: '开庭', val: '2' },
      { name: '办理财产保全手续', val: '3' },
      { name: '车辆信息', val: '4' },
      { name: '工商档案信息', val: '5' },
      { name: '婚姻档案信息', val: '6' },
      { name: '不动产档案信息', val: '7' },
      { name: '诉讼/执行档案信息', val: '8' },
      { name: '户籍信息', val: '9' },
      { name: '建设工程备案信息', val: '10' },
      { name: '实地查看涉案工程情况', val: '11' },
      { name: '实地查看被告经营情况', val: '12' }
    ],
    entrustRewardList:[],
    pageNum: 1,
    totalPage: '',
    cityName:""
  },

  //下拉执行
  onPullDownRefresh: function () {
    this.queryEntrustList();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.p_c[0] = provinceList;
    //初始化p_c
    // this.checkCity('上海');
    this.setData({ p_c: this.data.p_c })
    this.queryEntrustList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    

  },

  //根据省份获取城市列表
  checkCity: function (province) {
    let cityList = new Array();
    let provinceName = province || '';
    for (let key in cityData[provinceName]) {
      cityList.push(key)
    }
    // console.log("cityList=",cityList)
    lastCityList = cityList;
    this.data.p_c[1] = cityList
    this.setData({ p_c: this.data.p_c })
  },

  //查档地域选择
  pkIndex: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      pcIndex: e.detail.value,
      pageNum: 1,
      totalPage: ''
    })
    let procive = provinceList[e.detail.value[0]];
    let city = lastCityList[e.detail.value[1]];

    console.log("procive=", procive, city)
    let indexType = this.data.indexType;
    let typeVal = "";
    if (indexType != "") {
      typeVal = this.data.consultFileList[indexType].val;
    }
    this.setData({
      cityName: city
    });
    this.queryEntrustList(procive, city, typeVal, 1)
  },

  //触发列
  pkCol: function (e) {
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    let data = {
      p_c: this.data.p_c,
      pcIndex: this.data.pcIndex
    };
    //如果选择的是省则会改变市级数据
    if (e.detail.column == 0){
      let province = provinceList[e.detail.value];
      data.pcIndex[0] = e.detail.value;
      if (e.detail.value == 0){
        data.p_c[1] = [""];
        lastCityList = [""];
      }else{
        this.checkCity(province);
      }
      
    }else{
      data.pcIndex[1] = e.detail.value;
    }
    this.setData(data)
  },

  //查档类型选择
  bindConsultFileChange: function (e) {
    console.log('picker查档类型选择', e.detail.value)
    let index = e.detail.value;
    this.setData({
      indexType: index,
      pageNum: 1,
      totalPage: ''
    })
    let typeVal = this.data.consultFileList[e.detail.value].val;
    
    let procive = "", city = "";
    let pcIndex = this.data.pcIndex;
    let p_c = this.data.p_c;
    if (p_c[1][0]) {
      procive = provinceList[pcIndex[0]];
      city = lastCityList[pcIndex[1]];
    }
    this.queryEntrustList(procive, city, typeVal,1)
  },

  //滚动到底部触发
  lowerBottom: function (e) {
    console.log("底部=", e)
    let pageNum = this.data.pageNum;
    let totalPage = this.data.totalPage;

    let procive = "", city="";
    let pcIndex = this.data.pcIndex;
    let p_c = this.data.p_c;
    if (p_c[1][0]){
       procive = provinceList[pcIndex[0]];
       city = lastCityList[pcIndex[1]];
    }

    let indexType = this.data.indexType;
    let typeVal = "";
    if (indexType !=""){
      typeVal = this.data.consultFileList[indexType].val;
    }
    if (pageNum < totalPage) {
      this.queryEntrustList(procive, city,typeVal,pageNum * 1 + 1)
    } else {

    }
  },


  //去我的悬赏页面
  goMyReward: function(){
    wx.navigateTo({
      url: '../myReward/myReward',
    })
  },

  //案件悬赏详情
  goEntrustDet: function(e){
    if (app.globalData.identity){
      wx.showModal({
        title: '用户未登录',
        content: '是否去登陆',
        confirmText: "确定",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../reg/reg',
            })
          } else if (res.cancel) {
          }
        }
      })
      return
    }

    let entrustId = e.currentTarget.dataset.entrustid;
    console.log(e)
    wx.navigateTo({
      url: '../entrustDet/entrustDet?entrustId=' + entrustId,
    })
  },

  //查询委托尽调发布列表
  queryEntrustList: function (province, city, entrustType, pageNum) {
    let headerInfo = {
      pageNum: pageNum || "1",
      pageSize: "10",
      sessionId: app.globalData.sessionId,
    }
    let provinceName = "";
    if (province != '全国'){
      provinceName = province;
    }
    let bodyInfo = {
      status: "1",
      province: provinceName || "",
      city: city || "",
      entrustType: entrustType || ""
    }
    if (headerInfo.pageNum == 1) {
      this.setData({
        entrustRewardList: []
      });
    }
    console.log("bodyInfo=", bodyInfo)
    let _this = this;
    // wx.showLoading({ title: '加载中',mask:true});
    request.requestLogin_api("queryEntrustConsultList", headerInfo, bodyInfo, (obj) => {
      wx.stopPullDownRefresh();
      console.log("悬赏列表=",obj);
      wx.hideLoading(obj);
      if (obj.data.errorCode == '00'){
        let tempData = JSON.parse(obj.data.body);
        let totalPage = tempData.totalPage;
        let pageNum = tempData.pageNum;
        let totalNum = tempData.totalNum;
        let tempList = _this.data.entrustRewardList;
        if (tempData.consultVOList.length > 0){
          tempData.consultVOList.forEach(function(e){
            e.entrustName = formatUtil.getEntrustNameById(e.entrustType + '');
            // e.endDate = formatUtil.checkYearMonthDay(e.endDate);
            tempList.push(e)
          });
        }
        // console.log("悬赏列表=", tempList);
        _this.setData({
          entrustRewardList: tempList,
          pageNum: pageNum,
          totalPage: totalPage
        });
        
      }else{
        wx.showModal({
          title: "查询失败",
          content: obj.data.errorMsg,
          confirmText: '确定',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            } else if (res.cancel) {
            }
          }
        })
      }
    }, (obj) => {
      wx.stopPullDownRefresh();
      wx.hideLoading();
      wx.showModal({
        title: "加载失败",
        content: '请尝试下拉界面重新加载',
        confirmText: '确定',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})